
package com.kuyun.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.kuyun.base.base.Page;
import com.kuyun.base.mapper.BaseDictionaryMapper;
import com.kuyun.base.model.BaseDictionary;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.model.ZtreeBean;
import com.kuyun.base.service.BaseDictionaryService;
import com.kuyun.base.util.Constants;
import com.kuyun.base.util.IDUtil;

/**
 * 字典管理服务接口的实现类
 * 
 * @author 王坤
 * @date 创建时间：2016年6月18日 下午8:02:46
 */
@Service("baseDictionaryService")
public class BaseDictionaryServiceImpl implements BaseDictionaryService {

	/**
	 * 注入字典mapper对象
	 */
	@Resource
	private BaseDictionaryMapper baseDictionaryMapper;

	/**
	 * @author 王坤 增加字典信息
	 * @date 2016年6月19日上午12:19:33
	 *
	 * @param baseDictionary
	 * @return 0-false 1-success
	 */
	public String insert(BaseDictionary baseDictionary) {
		String result = "";
		baseDictionary.setId(IDUtil.getId());
		baseDictionary.setCreateUserId("1");
		baseDictionary.setCreateTime(new Date());
		if (StringUtils.isEmpty(baseDictionary.getBaseDicParent())) {
			baseDictionary.setBaseDicParent("0");
		}
		int num = baseDictionaryMapper.insertDictionary(baseDictionary);
		if (num == 1) {
			result = ValidFormResult.Y;
		} else {
			result = ValidFormResult.N;
		}
		return result;
	}

	/**
	 * @author 王坤 查询所有字典信息
	 * @date 2016年6月19日上午12:19:33
	 *
	 * @param baseDictionary
	 * @return 0-false 1-success
	 */
	public List<ZtreeBean> getDictionaryTree() {
		List<ZtreeBean> list = baseDictionaryMapper.getDictionaryTree();
		return list;
	}

	/**
	 * @author 王坤 更新字典信息
	 * @date 2016年6月19日下午7:55:27
	 *
	 * @param baseDictionary
	 * @return
	 */
	public String update(BaseDictionary baseDictionary) {
		String result = "";
		baseDictionary.setUpdateUserId("22");
		if (StringUtils.isEmpty(baseDictionary.getBaseDicParent())) {
			baseDictionary.setBaseDicParent("0");
		}
		baseDictionary.setUpdateTime(new Date());
		int num = baseDictionaryMapper.updateDictionary(baseDictionary);
		if (num == 1) {
			result = ValidFormResult.Y;
		} else {
			result = ValidFormResult.N;
		}
		return result;
	}

	/**
	 * 根据CODE查询字典对象
	 * 
	 * @author 唐靖
	 *
	 * @date 2016年6月25日下午2:39:31
	 *
	 * @param dicPrivilegeTypeMenu
	 * @return
	 */
	public BaseDictionary findByCode(String dicPrivilegeTypeMenu) {

		return baseDictionaryMapper.findByCode(dicPrivilegeTypeMenu);
	}

	/**
	 * @author 王坤
	 *
	 * @date 2016年6月29日下午10:18:46
	 *
	 * @return
	 */
	public Page<BaseDictionary> findByParentCode(Page<BaseDictionary> page, String parentId, String search) {
		int num = baseDictionaryMapper.findByParentCodeCount("root".equals(parentId) ? "0" : parentId, null);
		if (num == 0) {
			return page;
		}
		page.setTotal(num);
		List<BaseDictionary> list = baseDictionaryMapper.findByParentCode(page.getPageSize(),
				(int) (page.getPageNumber() - 1) * page.getPageSize(), "root".equals(parentId) ? "0" : parentId,
				search);
		page.setRows(list);
		return page;
	}

	/**
	 * 根据Parent查询字典信息总量
	 */
	public Integer findByParentCodeCount(String parentId, String search) {
		return baseDictionaryMapper.findByParentCodeCount(parentId, search);
	}

	/**
	 * 根据ID查询字典信息
	 */
	public BaseDictionary findByID(String id) {
		return baseDictionaryMapper.findById(id);
	}

	/**
	 * 删除字典信息
	 */
	public ValidFormResult deleteDic(String id) {
		ValidFormResult result = new ValidFormResult();
		int count = baseDictionaryMapper.findSonDic(id);
		if (count > 0) {
			result.setStatus(ValidFormResult.N);
			result.setInfo("存在子节点，不支持删除！");
		} else {
			baseDictionaryMapper.deleteDic(id);
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}

	/**
	 * 判断code是否存在
	 */
	public boolean checkCode(BaseDictionary baseDictionary, String id) {
		int num = baseDictionaryMapper.checkCode(baseDictionary);
		if (id != null) {
			BaseDictionary basedic = baseDictionaryMapper.findById(id);
			if (basedic.getBaseDicCode().equals(baseDictionary.getBaseDicCode())) {
				return true;
			}
		}
		if (num > 0) {
			return false;
		}
		return true;
	}

	public List<BaseDictionary> getDicByParentCode(String code) {

		return baseDictionaryMapper.getDicByParentCode(code);
	}

	public List<BaseDictionary> getDicByParentCodeAll(String code) {
		return baseDictionaryMapper.getDicByParentCodeAll(code);
	}
}
